package com.jd.yao.controller.common;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * ResPage.class
 * @Description 通用分页工具类
 * @Author wangwenbin16
 * @Date 2020/02/35
 */
@Getter
@Setter
public class ResPage<T> implements Serializable {
    /*** ***/
    private static final long serialVersionUID = 1L;
    /*** 初始化每页最大条数 ***/
    private static final int INIT_SIZE = 10;
    /**每页条数**/
    private int pageSize = INIT_SIZE;
    /**总条数**/
    private long totalCount;
    /**当前页数**/
    private int currentPage;
    /**分页数据源**/
    private List<T> data;
    /*** ***/
    public ResPage() {
    }
    /*** ***/
    public ResPage(int currentPage) {
        this.currentPage = currentPage;
    }
    /*** ***/
    public ResPage(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    /**
     *  获取当前页 第一条数据索引
     * @return
     */
    public int getStartIndex() {
        return (this.getCurrentPage() - 1) * this.pageSize;
    }
    /**
     *  获取当前页 最后一条数据索引
     * @return
     */
    public int getEndIndex() {
        return this.getCurrentPage() * this.pageSize;
    }

    /**
     * 当前页是否为首页
     * @return
     */
    public boolean isFirstPage() {
        return this.getCurrentPage() <= 1;
    }

    /**
     * 当前页是否为最后一页
     * @return
     */
    public boolean isLastPage() {
        return (long)this.getCurrentPage() >= this.getPageCount();
    }

    /**
     * 获取下一页 页码值
     * @return
     */
    public int getNextPage() {
        return this.isLastPage() ? this.getCurrentPage() : this.getCurrentPage() + 1;
    }

    /**
     * 获取上一页页码值
     * @return
     */
    public int getPreviousPage() {
        return this.isFirstPage() ? 1 : this.getCurrentPage() - 1;
    }

    /**
     * 获取当前页信息
     * @return
     */
    public int getCurrentPage() {
        if (this.currentPage == 0) {
            this.currentPage = 1;
        }
        return this.currentPage;
    }

    /**
     * 获取总页数
     * @return
     */
    public long getPageCount() {
        return this.totalCount % (long)this.pageSize == 0L ? this.totalCount / (long)this.pageSize : this.totalCount / (long)this.pageSize + 1L;
    }

    /**
     * 根据当前页  判断是否具有下一页
     * @return
     */
    public boolean hasNextPage() {
        return (long)this.getCurrentPage() < this.getPageCount();
    }

    /**
     * 根据当前页 判断是否具有上一页
     * @return
     */
    public boolean hasPreviousPage() {
        return this.getCurrentPage() > 1;
    }



}
